package com.xinyun.traceability.service.impl;

import com.xinyun.traceability.enity.po.CommodityPic;
import com.xinyun.traceability.enity.po.SysUser;
import com.xinyun.traceability.mapper.CommodityMapper;
import com.xinyun.traceability.mapper.CommodityPicMapper;
import com.xinyun.traceability.service.CommodityPicService;
import com.xinyun.traceability.utils.DateUtil;
import com.xinyun.traceability.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.FileCopyUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Collections;
import java.util.Date;
import java.util.List;

@Service
public class CommodityPicServiceImpl implements CommodityPicService {

    @Value("${commodity.picpath}")
    private String commodityPicpath;

    @Autowired
    CommodityPicMapper commodityPicMapper;


    @Override
    public R layuiUpload(String uuid, List<MultipartFile> multipartFiles, HttpServletRequest request) {
        if (StringUtils.isEmpty(uuid)) {
            return R.failed("参数错误");
        }
        if (CollectionUtils.isEmpty(multipartFiles)) {
            return R.failed("未获取到图片");
        }
        SysUser sysUser = (SysUser) request.getSession().getAttribute("sysUser");
        for (MultipartFile multipartFile : multipartFiles) {
            //图片后缀名
            String postfix = multipartFile.getOriginalFilename().substring(multipartFile.getOriginalFilename().lastIndexOf("."));
            String prefix = DateUtil.date2String(new Date());
            String path = uuid + File.separator + prefix + postfix;
            File file = new File(commodityPicpath + File.separator + path);
            if (!file.getParentFile().exists()) {
                file.getParentFile().mkdirs();
            }
            int copy = 0;
            try {
                copy = FileCopyUtils.copy(multipartFile.getInputStream(), new FileOutputStream(file));
            } catch (IOException e) {
                e.printStackTrace();
            }
            if (copy > 0) {
                //图片名称（不带后缀）
                String fileName = getFileName(multipartFile);
                CommodityPic commodityPic = new CommodityPic();
                commodityPic.setOriginalName(fileName);
                commodityPic.setBusinessId(sysUser.getId());
                commodityPic.setPicType(postfix);
                commodityPic.setCreateTime(new Date());
                commodityPic.setUpdateTime(new Date());
                commodityPic.setName(prefix + postfix);
                commodityPic.setUrl(path);
                commodityPic.setUuid(uuid);

                commodityPicMapper.insertSelective(commodityPic);
            }

        }
        return R.succeed("上传成功");
    }

    protected String getFileName(MultipartFile multipartFile) {
        return multipartFile.getOriginalFilename().substring(0, multipartFile.getOriginalFilename().lastIndexOf("."));
    }

    @Override
    public List<CommodityPic> selectCommodityPicByUuid(String uuid) {
        return commodityPicMapper.selectCommodityPicByUuid(uuid);
    }

    @Override
    public CommodityPic selectByPrimaryKey(Integer id) {
        return commodityPicMapper.selectByPrimaryKey(id);
    }

    @Override
    public void commodityPicView(HttpServletResponse response, Integer id) {
        CommodityPic commodityPic = commodityPicMapper.selectByPrimaryKey(id);
        if (commodityPic != null) {
            File file = new File(commodityPicpath + File.separator + commodityPic.getUrl());
            if (!file.exists()) {
                return;
            }
            try {
                response.setContentType("image/*");
                FileCopyUtils.copy(new FileInputStream(file), response.getOutputStream());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public int deleteCommodityPicByPrimaryKey(Integer id) {
        CommodityPic commodityPic = commodityPicMapper.selectByPrimaryKey(id);
        File file = new File(commodityPicpath + File.separator + commodityPic.getUrl());
        if (file.exists()) {
            file.delete();
        }
        return commodityPicMapper.deleteByPrimaryKey(id);
    }
}
